﻿using System;
using System.Collections.Generic;
using System.Linq;
using WickedFlame.Core;
using WickedFlame.Core.Shapes;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using WickedFlame.Core.History;

namespace WickedFlame.Util.Actions
{
    public class ConvertShapeToImageAction
    {
        public void DoAction(DrawingCanvas canvas)
        {
            var shapes = from VectorShape child in canvas.SelectedLayer.ShapeList
                            where child.IsSelected
                            select child;

            foreach (VectorShape shape in shapes)
            {
                if (!(shape is ImageShape))
                {
                    ImageShape imageShape = new ImageShape();
                    int shapeIndex = canvas.SelectedLayer.ShapeList.IndexOf(shape);
                    Rect bounds = shape.Bounds;

                    var command = new ConvertToImageCommand(
                        shape,
                        imageShape,
                        shapeIndex,
                        canvas.SelectedLayerIndex);


                    //set the shape to 0,0 so that the image can be drawn corectly
                    shape.Move(-(shape.Bounds.X - (shape.StrokeThickness)),
                        -(shape.Bounds.Y - (shape.StrokeThickness)));

                    DrawingVisual visual = new DrawingVisual();
                    DrawingContext dc = visual.RenderOpen();
                    shape.Draw(dc);
                    dc.Close();

                    System.IO.MemoryStream stream = new System.IO.MemoryStream();

                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    encoder.Interlace = PngInterlaceOption.On;
                    // Create a render bitmap and push the surface to it
                    RenderTargetBitmap renderBitmap =
                      new RenderTargetBitmap(
                        (int)(shape.Bounds.Width + shape.StrokeThickness + shape.StrokeThickness),
                        (int)(shape.Bounds.Height + shape.StrokeThickness + shape.StrokeThickness),
                        96d,
                        96d,
                        System.Windows.Media.PixelFormats.Pbgra32);

                    renderBitmap.Render(visual);

                    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                    encoder.Interlace = PngInterlaceOption.On;
                    // save the data to the stream
                    encoder.Save(stream);

                    stream.Seek(0, System.IO.SeekOrigin.Begin);

                    BitmapImage image = new BitmapImage();
                    image.BeginInit();
                    image.CacheOption = BitmapCacheOption.OnLoad;
                    image.StreamSource = stream;
                    image.EndInit();

                    // set all the properties of the new image
                    imageShape.Image = image;
                    imageShape.Move(bounds.X, bounds.Y);
                    command.SetNewState();

                    canvas.RemoveShape(shape);

                    canvas.InsertShape(shapeIndex, imageShape);
                    imageShape.Invalidate();
                    imageShape.IsSelected = true;

                    canvas.PushHistoryCommand(command);
                }
            }

            DrawingCanvas.DrawTrackers();
        }
    }
}
